package com.liunian.algorithmstudy.greedy;

public class CanCompleteCircuit134 {

	public int canCompleteCircuit(int[] gas, int[] cost) {
		int len = gas.length;
		int i = 0;
		while (i < len) {
			int gasSum = 0;
			int costSum = 0;
			int index = 0;
			while (index < len) {
				int j = (i + index) % len;
				gasSum += gas[j];
				costSum += cost[j];
				if (gasSum < costSum) {
					break;
				}
				index++;
			}
			if (index == len) {
				return i;
			} else {
				i = i + index + 1;
			}
		}
		return -1;
	}

	public static void main(String[] args) {
		CanCompleteCircuit134 circuit134 = new CanCompleteCircuit134();
		System.out.println(circuit134.canCompleteCircuit(new int[]{1, 2, 3, 4, 5}, new int[]{3, 4, 5, 1, 2}));
	}

}
